//
//  Utility.h
//  Dungeons and Princesses
//
//  Created by John Grange on 10/15/11.
//  Copyright (c) 2011 SD Networks. All rights reserved.
//

#ifndef Dungeons_and_Princesses_Utility_h
#define Dungeons_and_Princesses_Utility_h

#include <sstream>
#include <string.h>
#include <algorithm>
#include "Inventory.h"
#include <errno.h> // for errno
#include <sys/stat.h> // for stat and mkdir
#include <stdlib.h>
#include <stdio.h>

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"
#define GetCurrentDir _getcwd
#elif __linux__
#include <unistd.h>
#else
#include <direct.h>
#define GetCurrentDir _getcwd
#endif

//#include <direct.h>
//#define GetCurrentDir _getcwd


class Utility {
    private:

public:

    static void initSeed() {

        srand((unsigned)time(NULL));
    }
    static int getSeed(int max) {

        int returnValue;

        returnValue = rand() % max;

        return returnValue;

    }
    static string lowerCase(std::string stringToConvert)
    {
        std::string lowerCaseString = stringToConvert;
        transform (lowerCaseString.begin(), lowerCaseString.end(), lowerCaseString.begin(), ::tolower);

        return lowerCaseString;


    }

    static void getCommands(std::string commandToParse, vector<string> &commandsToPassBack) {

        //string command[size];
        string subCommand;
        istringstream iss(commandToParse);
        //int count = 0;
        while (getline (iss, subCommand, ' '))
        {
            commandsToPassBack.push_back(subCommand);

        }

    }

    static string removeGarbage(std::string stringToStrip) {

        stringToStrip.erase(stringToStrip.find_last_not_of(" \n\r\t")+1);

        return stringToStrip;
    }

    static void printListingOBject(Listing invObject) {

        std::cout << invObject.description << std::endl;
    }

    static string getCurrentWorkingDir()
    {
        char cCurrentPath[FILENAME_MAX];

        // ----------------------------------------------------------------------------
        // This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
        #ifdef __APPLE__
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
        //char path[PATH_MAX];
        if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)cCurrentPath, PATH_MAX))
        {
            string msg = "Error: something wierd has happened";
            throw msg;
        }
        CFRelease(resourcesURL);
        chdir(cCurrentPath);
        // ----------------------------------------------------------------------------
        #elif __linux__
        if(getcwd(cCurrentPath,FILENAME_MAX) == NULL){
            // error
        }
            if(chdir(cCurrentPath)){
            // error
        }
        #else
        if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
        {
            string msg = "Error: something wierd has happened";
            throw msg;
        }
        #endif

        //return cCurrentPath;
	std::string cPath(cCurrentPath);
        return cPath;
    }

    static bool createDir(std::string dirToCreate) {


        struct stat st;
        if (stat(dirToCreate.c_str(), &st) == 0) {
            return true;
        }
        #if defined __APPLE__ || __linux__
        if (mkdir(dirToCreate.c_str(), S_IRWXU|S_IRWXG) != 0) {
            string msg = "Error: Directory " + dirToCreate + " was not created.";
            throw msg;
        }
        return false;
        #else
        if(mkdir(dirToCreate.c_str()) != 0)
        {
            string msg = "Error: Directory " + dirToCreate + " was not created.";
            throw msg;
        }
        return false;
        #endif

    }

    static bool charIsNotNumeric(char c)
    {
        return !(c > 47 && c < 58);
    }

    static bool isValidFileName(string filename)
    {
        #warning This code is funky, if someone knows a better way...
        char badDelims[] = {'\\', '/', ' '};
        for(int i = 0; i < filename.size(); ++i)
        {
            for(int j = 0; j < 3; ++j)
            {
                if(filename[i] == badDelims[j])
                {
                    return false;
                }

            }
        }
        return true;
    }

};
#endif
